const mongoose = require("mongoose");

const userName = "root";
const password = "000000";
const database = "express-blog";
const host = "192.168.83.129";
const port = "27017";
const url = `mongodb://${userName}:${password}@${host}:${port}/${database}?authSource=admin`;

async function connectDB() {
  try {
    await mongoose.connect(url);
    console.log("\x1b[32m✅ MongoDB 连接成功\x1b[0m");
  } catch (err) {
    console.error("\x1b[31m❌ MongoDB 连接失败:\x1b[0m", err);
    throw err;
  }
}

async function disconnectDB() {
  try {
    await mongoose.disconnect();
    console.log("\x1b[32m✅ MongoDB 关闭成功\x1b[0m");
  } catch (err) {
    console.error("\x1b[31m❌ MongoDB 关闭失败:\x1b[0m", err);
    throw err;
  }
}

module.exports = {
  connectDB,
  disconnectDB,
};
